<?php
class MaintenanceMenuController extends Controller {
    public $viewClass = 'Smarty';
    public $uses = array('KanriModel', 
    						'ShipperDiffModel', 
    						'ProductDiffModel',
							'ExportMonthLyDataModel',
							'ImportExportMonthLyDataSModel'
							);

    public function index() {
    }

	// kanri start >>> 
    public function frmKanri() {
        // get data
        $arrKanri = $this->KanriModel->getInfoFrmKanri();
        $this->set('arrKanri', $arrKanri);
        $this->render('/MaintenanceMenu/frmKanri');
    }
	// <<< kanri end 
	// product diff start >>> 
    public function frmProductDiff() {
        // get data
        $arrProducts = $this->ProductDiffModel->getInfoFrmProductDiff();
        $this->set('arrProducts', $arrProducts);
        $this->render('/MaintenanceMenu/frmProductDiff');
    }
	// <<< product diff end 	
	
	// shipping diff start >>> 
    public function frmShipperDiff() {
        // get data
        $arrShippers = $this->ShipperDiffModel->getInfoFrmShipperDiff();
        $this->set('arrShippers', $arrShippers);
        $this->render('/MaintenanceMenu/frmShipperDiff');
    }

    public function frmShipperDiffPdfHtml() {
        // get data
        $arrShippers = $this->ShipperDiffModel->getInfoFrmShipperDiffReport();
        $this->set('arrShippers', $arrShippers);
        $this->render('/MaintenanceMenu/frmShipperDiffPdfHtml');
    }
	
    public function frmShipperDiffPdf() {
    	// Set temp file name
		$tmpFile = sprintf(PDF_RECEIPT_FILE_DIR, microtime(true)*10000 . rand(1,100));
		// Set HTML page
		$htmlPage = BASE_URL . '/frmShipperDiffPdfHtml';
		// Set pdf option
		// $pdfOption = '-s A4 --margin-right 0 --margin-left 0 --zoom 1 --footer-html ' . BASE_URL . '/frmFooter';
		$pdfOption = PDF_MARGIN_OPTION;
		// Excute
		$result = exec('"'.HTML_TO_PDF_EXE_PATH . '" ' . $pdfOption . ' ' . '"' . $htmlPage . '"' . ' ' . '"'.$tmpFile.'"', $output, $return_var);
		if (file_exists($tmpFile)) {
			$this->response->file($tmpFile);
		    $this->response->header('Content-Disposition', 'inline');
		    return $this->response;
		} else {
			$this->redirect('/frmShipperDiff');
		}
    }
	// <<< shipper end	
	public function backup() {
		 // set timeout + memory
        ini_set("max_execution_time","0");
        ini_set('memory_limit', '-1');      	
        $this->layout = false;
        $this->autoRender = false;
		$ret = true;		
		// call exec
		if ($this->request->is('ajax')) {
			exec(CMD_RUN_BATCH_BACKUP, $out, $ret);
			foreach ($out as $item) {
				if (strpos($item, 'Exception') !== false) {
					echo $item;
					exit();
				}
			}
			echo '1';
			exit();
		}
		echo 'Internal System Error';
		exit();
	}
    // restore DB from file
    public function restore() {
		 // set timeout + memory
        ini_set("max_execution_time","0");
        ini_set('memory_limit', '-1');      	
        $this->layout = false;
        $this->autoRender = false;
		$ret = true;
		if ($this->request->is('ajax')) {
			try {
				// backup shipper
				$ret = $this->ExportMonthLyDataModel->resShipper();
				// backup area
				$ret &= $this->ExportMonthLyDataModel->resArea();
				// backup shipping fee
				$ret &= $this->ExportMonthLyDataModel->resShipping();
				// backup kanri
				$ret &= $this->ExportMonthLyDataModel->resKanri();
				// backup diff shipper
				$ret &= $this->ExportMonthLyDataModel->resDiffShipper();
				// backup diff product
				$ret &= $this->ExportMonthLyDataModel->resDiffProduct();
				// ob_get_clean();
				// マスタ自動生成処理
				$this->runAutoProcess();
				if ($ret) {
		       		echo '1';
					die();
				} else {
					die();					
				} 
	        } catch (Exception $ex) {
	           echo $ex->getMessage();
			   die();
	        }
        }
        echo 'Internal System Error';
        die();
    }	
	// マスタ自動生成処理
    private function runAutoProcess(){
        //1. ｍ_エリア生成03（エリアマスタとの不一致）
        //2. Q ｍ_エリア生成04（エリアマスタに追加）
        $this->ImportExportMonthLyDataSModel->insArea();
        //3. ｍ_荷主マスタ生成03（荷主マスタとの不一致）
        //4. ｍ_荷主マスタ生成04（荷主マスタに追加）
        //5. ｍ_荷主マスタ生成05（レコードカウント）
        //6. m_荷主マスタ生成06（集約データ削除）
        //7. m_荷主マスタ生成07(集約データ追加)
        //8. m_荷主マスタ生成08（名称置換）
        $this->ImportExportMonthLyDataSModel->insShipper();
        //9. ｍ_配送手数料生成02（配送手数料マスタとの不一致）
        //10. ｍ_配送手数料生成03（追加）
        $this->ImportExportMonthLyDataSModel->insShippingFee();
    }	
}
